﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Collections.ObjectModel;

namespace ScaffoldIt.Configuration
{
    [XmlType("PresetCategory")]
    public class PresetCategory 
    {
        [XmlAttribute]
        public string Category { get; set; }

        private string templates;
        private string[] splittedtemplates;

        public string[] GetTemplates()
        {
            if (splittedtemplates == null)
                return new string[0];

            return splittedtemplates;
        }

        [XmlAttribute]
        public string Templates
        {
            get
            {
                return this.templates;
            }
            set
            {
                this.templates = value;
                if (templates != null)
                {
                    splittedtemplates = templates.Split(',');
                    for (int i = 0; i < splittedtemplates.Length; i++ )
                    {
                        splittedtemplates[i] = splittedtemplates[i].Trim();
                    }
                }
                else splittedtemplates = null;
            }
        }

        

        private List<PresetCategory> categories = new List<PresetCategory>();
        [XmlArray("PresetCategories")]
        public List<PresetCategory> Categories
        {
            get
            {
                return this.categories;
            }
            set
            {
                this.categories = value;
            }
        }

        public bool HasTemplateName(string name)
        {
            if (string.IsNullOrEmpty(name) || splittedtemplates == null) 
                return false;

            return splittedtemplates.Any(s => string.Compare(s, name, true) == 0);
        }

        internal void AppendTemplate(string p)
        {
            if (string.IsNullOrEmpty(Templates))
            {
                Templates = p;
            }
            else if (!splittedtemplates.Any(t => t == p))
            {                
                Templates = Templates + "," + p;
            }
        }

        internal void RemoveTemplate(string templName)
        {
            var templates = splittedtemplates.Where(t => t != templName);

            this.Templates = Collapse(templates.OrderBy(s => s));
        }

        private string Collapse(IEnumerable<string> templates)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var template in templates)
            {
                if (sb.Length > 0) sb.Append(",");
                sb.Append(template);
            }
            return sb.ToString();
        }

        internal void RenameTemplate(string oldName, string p)
        {
            for (int i = 0; i < splittedtemplates.Length; i++ )
            {
                if (splittedtemplates[i] == oldName) splittedtemplates[i] = p;
            }
            this.Templates = Collapse(splittedtemplates.OrderBy(s => s));
        }
    }
}
